package com.xingzhi.crm.mapper;

import com.zxcms.user.bean.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by daniel.luo on 2018/6/12.
 */
@Repository
public interface CmsMapper {
    @SelectProvider(type = SqlProvider.class, method = "getTotalLen")
    CourseLen getTotalLen(Map<String, Object> mapParam);

    @SelectProvider(type = SqlProvider.class, method = "delUseless")
    Object delUseless(Map<String, Object> mapParam);

    @Transactional
    @Update("update course_shot set total_len = #{len}, total_num = #{num} where cid = #{cid}")
    int updateTotalLen(Map<String, String> mapParam);

    @Transactional
    @Update("UPDATE course_shot SET status=#{status} WHERE cid=#{cid}")
    int updateCourseShotStatus(Map<String, String> mapParam);

    @Transactional
    @Select("SELECT * FROM personage WHERE name LIKE CONCAT('%',#{name},'%')")
    List<Personage> likePersonage(Map<String, String> mapParam);

    @Transactional
    @Update("update manager set name = #{name},  phone_num = #{phone_num}, email = #{email}, img_url = #{img_url} where uid = #{uid}")
    int modifyManager(Map<String, String> mapParam);

    @Transactional
    @Select("SELECT uid, account, password, name, phone_num, email, role_type, status, img_url, err_time, err_count FROM manager WHERE phone_num = #{phone_num}")
    List<User> getPhoneNumManager(String phone_num);

    @Transactional
    @Select("SELECT uid, account, password, nickname as name, phone_num, email, role_type, status, img_url, err_time, err_count FROM student WHERE phone_num = #{phone_num}")
    List<User> getPhoneNumStudent(String phone_num);

    @Transactional
    @Update("update manager set password = #{newpwd} where uid = #{uid}")
    int updateManagerPassword(Map<String, String> mapParam);

    @Transactional
    @Select("select count(*) from study where cid = #{cid}")
    int getStudyCount(String cid);

    @Transactional
    @Delete("delete from course_shot where cid = #{cid}")
    int removeCourse(String cid);

    @Transactional
    @Select("SELECT name , phone_num, email, img_url FROM manager WHERE uid=#{uid}")
    Manager getManagerOneByUid(String uid);

    @Transactional
    @Select("SELECT password FROM manager WHERE uid=#{uid}")
    String getManagerPwdByuid(String uid);

    @SelectProvider(type = SqlProvider.class, method = "getAdminActionRecord")
    List<Record> getAdminActionRecord(Map<String, String> mapParam);

    @Transactional
    @Select("SELECT c.cid, c.code, c.cname, s.create_time, s.add_mode FROM study as s LEFT JOIN course_shot as c ON c.cid = s.cid WHERE  s.uid=#{uid}")
    List<Study> getStudy(String uid);

    @Transactional
    @Delete("DELETE FROM study WHERE uid=#{uid} and cid={cid}")
    int delectStudyOne(Map<String, String> mapParam);

    @Transactional
    @Select("select  from course_shot where cid = #{cid}")
    String getCourseShotByCid(String cid);

    @Transactional
    @Select("SELECT uid, nickname as name, phone_num, create_time, real_name, id_card FROM student WHERE uid=#{uid}")
    StudentOne getStudentOneByUid(String uid);

    @Transactional
    @Update("update course_ware set step = '2', err_count = '0' where encrypt_key = #{key}")
    int updateCourseVideoEncryptStatusOk(String key);

    @Transactional
    @Update("update course_ware set step = '0', err_count = #{err_count} where encrypt_key = #{key}")
    int updateCourseVideoEncryptStatusFail(Map<String, String> mapParam);

    @Transactional
    @Select("select encrypt_key, COALESCE(err_count, '0') from course_ware where encrypt_key = #{key}")
    List<CourseEncrypt> getCourseByEncryptkey(String key);

    @Transactional
    @Update("update course_ware set encrypt_key = #{encrypt_key}, encrypt_pwd = #{encrypt_pwd}, step = '1', encrypt_time = #{encrypt_time}, err_count = '0' where url = #{url}")
    int setEncryptInfo(Map<String, String> mapParam);

    @Transactional
    @Update("update course_ware set step = '0', err_count = #{err_count} where url = #{url}")
    int updateCourseVideoEncryptStatusFailByUrl(Map<String, String> mapParam);

    @Transactional
    @Select("select encrypt_key, COALESCE(err_count, '0') as err_count from course_ware where url = #{url}")
    List<CourseEncrypt> getCourseByEncryptkeyByUrl(String url);

    @Transactional
    @Select("select ware_id, url, ware_name, cid, encrypt_key, encrypt_pwd, step, len, type, comment from course_ware where ware_id = #{ware_id}")
    CourseWareModel getCourseWareById(String ware_id);

    @Transactional
    @Update("update course_ware set encrypt_pwd = #{encrypt_pwd}, encrypt_time = #{encrypt_time}, step = '1' where ware_id = #{ware_id}")
    int updateEncryptTime(Map<String, String> mapParam);

    @Transactional
    @Select("select ware_id, url, ware_name, cid, encrypt_key, encrypt_pwd, encrypt_time, step, err_count, len, type, comment from course_ware where step = '1'")
    List<CourseWareModel> getAllCourseWareOnEncrypt();

    @Transactional
    @Select("SELECT c.cid, c.code, c.cname as course_name, c.desc_info, c.img_url, c.status, c.create_time, c.update_time, c.uid,  m.name as user_name, case when c.uid = #{uid} then '1' else '0' end as auth FROM course_shot as c LEFT JOIN manager as m ON m.uid = c.uid WHERE cname LIKE CONCAT('%',#{cname},'%') ORDER BY create_time DESC")
    List<CourseGet> getCourseShotLikeName(Map<String, String> mapParam);

    @Transactional
    @Delete("delete from course_ware where cid = #{cid}")
    int delCourseWare(String cid);

    @Transactional
    @Delete("delete from course_ware_relation where cid = #{cid}")
    int delCourseWareRelation(String cid);

    @Transactional
    @Select("select node_id, desc_info, ware_id from course_ware_relation where node_id = #{node_id}")
    CourseWareRelation getCourseWareRelation(String node_id);

    @Transactional
    @Select("select ware_id, ware_name, cid, url, encrypt_key, encrypt_pwd, step, len, type, comment from course_ware where ware_id = #{ware_id}")
    CourseWareModel getCourseWareByWareId(String ware_id);

    @Transactional
    @Insert("insert into course_ware values(#{ware_id}, #{cid}, #{ware_name}, #{url}, #{len}, #{type}, #{uid}, #{comment}, #{update_time}, #{create_time}, #{encrypt_key}, #{encrypt_time}, #{step}, #{err_count}, #{encrypt_pwd})")
    int addCoursewWare(Map<String, String> mapParam);

    @Transactional
    @Update("update course_shot set catalog = #{catalog}, uid = #{uid}, user_name = #{user_name}, total_num = #{total_num}, total_len = #{total_len} where cid = #{cid}")
    int updateCourseShot(Map<String, String> mapParam);

    @Transactional
    @Select("select total_num, total_len from course_shot where cid = #{cid}")
    CourseShotImport getTnumTlen(String cid);
}
